package io.github.shenbinglife.springboot2.layui.common.controller;

import io.github.shenbinglife.springboot2.layui.admin.entity.User;
import io.github.shenbinglife.springboot2.layui.common.model.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * ViewController
 *
 * @author shenbing
 * @date 2021/4/12 23:01
 */
@RequestMapping("view")
@Controller
public class ViewController {

    @RequestMapping("test")
    @ResponseBody
    public Result test() {
        return Result.success();
    }

    @RequestMapping("/**")
    public String view(@SessionAttribute(name = "user", required = false) User user,
                       HttpServletRequest request) {
        String attribute = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        return attribute.replaceFirst("/view", "");
    }
}
